package org.example.common.dubbo.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.example.common.core.constants.GrayConstant;
import org.example.common.core.utils.ribbon.GrayRequestContextHandler;

/**
 * 灰度标签透传过滤器
 */
@Slf4j
@Activate(group = {CommonConstants.CONSUMER}, order = Integer.MAX_VALUE)
public class DubboGrayTagConsumerFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        try {
            Boolean grayTag = GrayRequestContextHandler.getGrayTag();
            if(grayTag){
                RpcContext.getContext().setAttachment(GrayConstant.GRAY_HEADER,grayTag.toString());
            }
        }finally {
            GrayRequestContextHandler.remove();
        }
        return invoker.invoke(invocation);
    }

}
